package c.j.a.d.b.a;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.os.Bundle;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/* compiled from: AppStatusManager */
public class a {
    public Application a;
    public final List<b> b = new ArrayList();
    public int c;
    public volatile int d = -1;
    public final ActivityLifecycleCallbacks e = new a();

    /* compiled from: AppStatusManager */
    public class a implements ActivityLifecycleCallbacks {
        public void onActivityCreated(Activity activity, Bundle bundle) {
        }

        public void onActivityDestroyed(Activity activity) {
        }

        public void onActivityPaused(Activity activity) {
        }

        public void onActivityResumed(Activity activity) {
            a aVar = a.this;
            int i = aVar.c;
            aVar.c = activity != null ? activity.hashCode() : i;
            if (i == 0) {
                a.b(a.this);
            }
        }

        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
        }

        public void onActivityStarted(Activity activity) {
            a aVar = a.this;
            int i = aVar.c;
            aVar.c = activity != null ? activity.hashCode() : i;
            if (i == 0) {
                a.b(a.this);
            }
        }

        public void onActivityStopped(Activity activity) {
            if (activity != null) {
                int hashCode = activity.hashCode();
                a aVar = a.this;
                if (hashCode == aVar.c) {
                    hashCode = 0;
                    aVar.c = 0;
                    Objects.requireNonNull(aVar);
                    c.j.a.d.b.g.a.e("AppStatusManager", "dispatchAppBackground");
                    aVar.d = 0;
                    Object[] d = aVar.d();
                    if (d != null) {
                        int length = d.length;
                        while (hashCode < length) {
                            ((b) d[hashCode]).c();
                            hashCode++;
                        }
                    }
                }
            }
        }
    }

    /* compiled from: AppStatusManager */
    public interface b {
        void b();

        void c();
    }

    /* compiled from: AppStatusManager */
    public static class c {
        public static final a a = new a(null);
    }

    public a(a aVar) {
    }

    public static void b(a aVar) {
        Objects.requireNonNull(aVar);
        c.j.a.d.b.g.a.e("AppStatusManager", "dispatchAppForeground");
        aVar.d = 1;
        Object[] d = aVar.d();
        if (d != null) {
            for (Object obj : d) {
                ((b) obj).b();
            }
        }
    }

    public void a(b bVar) {
        if (bVar != null) {
            synchronized (this.b) {
                if (!this.b.contains(bVar)) {
                    this.b.add(bVar);
                }
            }
        }
    }

    public boolean c() {
        int i = this.d;
        if (i == -1) {
            Object obj = null;
            try {
                Class cls = Class.forName("android.app.ActivityThread");
                Object invoke = cls.getMethod("currentActivityThread", new Class[0]).invoke(null, new Object[0]);
                Field declaredField = cls.getDeclaredField("mActivities");
                declaredField.setAccessible(true);
                Map map = (Map) declaredField.get(invoke);
                if (map != null) {
                    if (map.size() != 0) {
                        Class cls2 = null;
                        Field field = cls2;
                        for (Object next : map.values()) {
                            if (cls2 == null) {
                                cls2 = next.getClass();
                            }
                            if (field == null) {
                                field = cls2.getDeclaredField("paused");
                            }
                            field.setAccessible(true);
                            if (!field.getBoolean(next)) {
                                declaredField = cls2.getDeclaredField("activity");
                                declaredField.setAccessible(true);
                                obj = (Activity) declaredField.get(next);
                                break;
                            }
                        }
                    }
                }
            } catch (Throwable th) {
                th.printStackTrace();
            }
            if (this.d == -1) {
                if (obj != null) {
                    this.c = obj.hashCode();
                    this.d = 1;
                } else {
                    this.d = 0;
                }
            }
            i = this.d;
        }
        if (i == 1) {
            return true;
        }
        return false;
    }

    public final Object[] d() {
        Object[] toArray;
        synchronized (this.b) {
            toArray = this.b.size() > 0 ? this.b.toArray() : null;
        }
        return toArray;
    }
}
